package com.naiterui.ehp.bs.emr.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.naiterui.common.redis.RedisUtil;
import com.naiterui.ehp.bp.bo.patient.FamilyBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.emr.entity.mongo.health.PatientHealthRecord;
import com.naiterui.ehp.bs.emr.exception.ExceptionCodes;
import com.naiterui.ehp.bs.emr.feign.IPatientFeignClient;
import com.naiterui.ehp.bs.emr.repository.mongo.PatientHealthRecordRepository;
import com.naiterui.ehp.bs.emr.service.IPatientHealthRecordService;
import com.naiterui.ehp.bs.emr.utils.Constants;
import com.naiterui.ehp.bs.emr.vo.health.HealthRecordVO;

/**
 * @Description 健康档案
 * @Author gongbaoqiang
 * @Date 2020/6/3 2:30 PM
 */
@Service
public class PatientHealthRecordSeviceImpl implements IPatientHealthRecordService {

    @Autowired
    private PatientHealthRecordRepository recordRepository;

    @Autowired
    private IPatientFeignClient patientFeignClient;

    @Override
    public void cacheRecord(Long familyId, HealthRecordVO recordVO) throws BusinessException {
        String key = Constants.HEALTH_RECORD_CACHE_KEY + familyId;
        RedisUtil.valueOps().set(key, recordVO);
    }

    @Override
    public void submit(Long familyId, HealthRecordVO recordVO) throws BusinessException {
        //保存档案
        FamilyBO familyBO = this.patientFeignClient.getPatientFamily(familyId);
        if (familyBO == null || familyBO.getId() == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        PatientHealthRecord record = JSON.parseObject(JSON.toJSONString(recordVO), PatientHealthRecord.class);
        PatientHealthRecord oldRecord = this.recordRepository.findFirstByFamilyId(familyId);
        if (oldRecord == null) {
            record.setCreatedAt(new Date());
        } else {
            record.setId(oldRecord.getId());
        }

        record.setFamilyId(familyId);
        record.setPatientId(familyBO.getPatientId());
        record.setRealation(familyBO.getRelation());
        record.setChangedAt(new Date());

        if (record.getId() == null) {
            this.recordRepository.save(record);
        } else {
            this.recordRepository.update(record);
        }

        //清楚缓存
        String key = Constants.HEALTH_RECORD_CACHE_KEY + familyId;
        RedisUtil.keyOps().delete(key);
    }

    @Override
    public HealthRecordVO detail(Long familyId) throws BusinessException {
        HealthRecordVO recordVO = null;
        PatientHealthRecord record = this.recordRepository.findFirstByFamilyId(familyId);
        if (record == null) {
            //取草稿数据
            String key = Constants.HEALTH_RECORD_CACHE_KEY + familyId;
            Object obj = RedisUtil.valueOps().getObject(key);
            if (obj != null) {
                recordVO = (HealthRecordVO) obj;
                recordVO.setStatus(HealthRecordVO.STATUS_CACHE);
            }
        } else {
            recordVO = JSON.parseObject(JSON.toJSONString(record), HealthRecordVO.class);
            recordVO.setStatus(HealthRecordVO.STATUS_SUBMIT);
        }
        return recordVO;
    }

    @Override
    public List<Long> findFamilyIdsByPatient(Long patientId) throws BusinessException {
        List<PatientHealthRecord> list = this.recordRepository.findByPatientId(patientId);
        List<Long> familyIds = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(list)) {
            familyIds = list.stream().map(PatientHealthRecord::getFamilyId).collect(Collectors.toList());
        }
        return familyIds;
    }
}
